import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,//用户是否登录
		userInfo: {},//用户信息
		buyList:[],//订单确定页面所需的商品数据
	},
	mutations: {
		login(state, saveData) {
			state.hasLogin = true;
			state.userInfo = saveData;
            uni.setStorage({//缓存用户登陆状态
                key: 'hasLogin',
                data: true
            });
            uni.setStorage({//缓存用户登陆状态
			    key: 'userInfo',  
			    data: saveData
			}) 
		},
		logout(state) {
			state.hasLogin = false;
			state.userInfo = {};
            uni.removeStorage({
                key: 'hasLogin'
            })
			uni.removeStorage({  
                key: 'userInfo'  
            })
		},
		buyList(state, saveData){
			state.buyList = saveData;
			uni.setStorage({//缓存订单确定页面所需的商品数据
                key: 'buyList',
                data: saveData
            })
        },
        clearBuyList(state){
            state.buyList = [];
            uni.removeStorage({
                key: 'buyList'
            })
        }
	},
	actions: {
	
	},
	getters: {
		hasLogin(state) {
			return state.hasLogin
		},
		userInfo(state) {
			return state.userInfo
		},
        buyList(state) {
            return state.buyList
        }
	}
})

export default store
